/**
 * TemplateService.java
 *
 * Copyright (c) 2009 prjmanager All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of prjmanager .
 **/
package ro.prjmanager.service;


import ro.prjmanager.core.service.AbstractService;
import ro.prjmanager.core.service.Service;
import ro.prjmanager.dao.DbSchemaDao;
import ro.prjmanager.dao.ProjectDbDao;
import ro.prjmanager.model.*;

import java.util.List;

/**
 * This class is business logic for template table
 * It manages all transaction maded to template table
 * @author Viorel Tacliciu
 * @version 0.0.1
 **/


public class TemplateService  extends AbstractService<Template,Integer> implements Service<Template,Integer> {


    private DbSchemaDao dbSchemaDao;
    private ProjectDbDao projectDbDao;




    public DbSchemaDao getDbSchemaDao() {
        return dbSchemaDao;
    }

    public void setDbSchemaDao(DbSchemaDao dbSchemaDao) {
        this.dbSchemaDao = dbSchemaDao;
    }

    public ProjectDbDao getProjectDbDao() {
        return projectDbDao;
    }

    public void setProjectDbDao(ProjectDbDao projectDbDao) {
        this.projectDbDao = projectDbDao;
    }

    public void create(Template user){
       String sqlDbSchema="select * from db_schema where fk_database="+((DataBases)dao.getUserSession().getDataBases()).getId()+
                " and fk_schema="+((Schema)dao.getUserSession().getSchema()).getId();
        DbSchema dbSchema=dbSchemaDao.find(sqlDbSchema).get(0);
        String sqlProjectDb="select * from project_db where fk_project="+((Projects)dao.getUserSession().getProject()).getId()+
                " and fk_db_schema="+dbSchema.getId();
        ProjectDb dbProjects=projectDbDao.find(sqlProjectDb).get(0);
        user.setFkProjectDb(dbProjects);      
       dao.create(user);
    }


    public void delete(Integer id){
        Template user =new Template();
        user.setId(id);
        dao.delete(user);
    }

    public Template load(Integer id){
         return dao.find(id,false);
    }

    public List<Template> listPag(int startPage, int resPerPage) {
        return dao.listPag(startPage, resPerPage);
    }

    public List<Template> listPag(int startPage, int resPerPage, String orderCol, boolean asc, Object... otherItems) {
        return dao.listPag(startPage, resPerPage,orderCol, asc, otherItems);
    }

    public List<Template> list() {
        return dao.findAll();
    }

    public List<Template> listPagFilter(int startPage, int resPerPage, String filter) {


        return dao.listPagFilter(startPage, resPerPage, filter);
    }

    public int no(String filter) {
        return dao.no(filter);
    }

    public void update(Template entity) {
        Template templDb=dao.find(entity.getId(),false);
        if(entity.getFileGenExt()!=null)
            templDb.setFileGenExt(entity.getFileGenExt());
        if(entity.getFileGenName()!=null)
            templDb.setFileGenName(entity.getFileGenName());
        if(entity.getName()!=null)
            templDb.setName(entity.getName());
        if(entity.getReplacer()!=null)
            templDb.setReplacer(entity.getReplacer());
        if(entity.getVmFile()!=null&&entity.getVmFile().length>0)
            templDb.setVmFile(entity.getVmFile());
         templDb.setNeedUpdate(entity.isNeedUpdate());

        dao.update(templDb);
    }


}
